using System;
using System.Configuration;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Catalyst.Caching.Configuration
{
	[Serializable]
	public class CatalystCachingSection : ConfigurationSection
	{
		private CacheElementCollection m_Caches;

		[ConfigurationProperty("caches", IsDefaultCollection = true)]
		//[ConfigurationCollection(typeof(CacheElementCollection),
		//    AddItemName = "add",
		//    ClearItemsName = "clear",
		//    RemoveItemName = "remove")]
		public CacheElementCollection Caches
		{
			get 
			{
				if (m_Caches == null)
				{
					m_Caches = (CacheElementCollection)base["caches"];
				}

				return m_Caches; 
			}
			set { m_Caches = value; }
		}

		#region ISerializable Members

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Caches", Caches);
		}

		#endregion

		public CatalystCachingSection()
		{
		}

		protected CatalystCachingSection(SerializationInfo info, StreamingContext context)
        {
			if (info == null)
			{
				throw new ArgumentNullException("info");
			}

			m_Caches = (CacheElementCollection)info.GetValue("Caches", typeof(CacheElementCollection));            
        }

		[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			if (info == null)
			{
				throw new ArgumentNullException("info");
			}

			GetObjectData(info, context);
		}
	}
}